﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using UI.Lib;
using Components;

namespace UI.ShopManagement
{
    public partial class FrmBas1a40Medit : UI.ParentForm.BaseMedit
    {
        public FrmBas1a40Medit(string modulecode, string action, string text, string key)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.Text = text;
            this.Key = key;

            //回写的方法
            this.SaveCallFuntion = "Bas1a40Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.allowDtlEmpty = false; //可能用户一次想把全部的记录删除
            this.MaxStateFlag = true;
            if (!string.IsNullOrEmpty(this.Key))
            {
                LoadData();
                IsAfterSaveCloseWin = true;
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("sellercode" == name)
            {
                where = string.Format(" status ='1' {0} ", Context.GetAppKeyWhere("usercode", true));
            }
            else if ("levercode" == name || "areacode" == name || "shopcode" == name)
            {
                if ("areacode" == name || "shopcode" == name) //区域代码：如果合同级别为区域则在此选择区域，否则为空;
                {
                    string levercode = Validation.ToString(this.tbSummary.GetValue("levercode"));
                    if (string.IsNullOrEmpty(levercode))
                    {
                        MessageBox.Show("请先录入[级别代码]!", "提示");
                        this.tbSummary.Focus("levercode");
                        return false;
                    }
                }

                if ("shopcode" == name) //限制门店的数据源，必须属于某个销售商下的门店
                {
                    string sellerid = Validation.ToString(this.tbSummary.GetValue("slistid"));
                    if (string.IsNullOrEmpty(sellerid))
                    {
                        MessageBox.Show("请先选择销售商！", "提示");
                        this.tbSummary.Focus("sellercode");
                        return false;
                    }

                    where = string.Format("slistid = '{0}' {1} ", sellerid, Context.GetAppKeyWhere("muser", true));
                }

                where = Context.GetAppKeyWhere("muser");
            }

            return true;
        }

        public override void AfterPopWin(string name, Hashtable data, bool ischange)
        {
            if ("levercode" == name && ischange)
            {
                string levercode = Validation.ToString(this.tbSummary.GetValue("levercode"));
                this.tbSummary.Enable("areacode", levercode == "1"); //区域代码：如果合同级别为区域则在此选择区域，否则为空;
                this.tbSummary.Enable("shopcode", levercode == "2");

                //清空区域和门店
                this.tbSummary.ClearItem("areacode");
                this.tbSummary.ClearItem("areaname");
                this.tbSummary.ClearItem("alistid");

                this.tbSummary.ClearItem("shopcode");
                this.tbSummary.ClearItem("shopname");
                this.tbSummary.ClearItem("splistid");
            }
        }

        protected override void PageInit()
        {
            this.tbSummary.DefaultWidth = 130;
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("chargecode" == name)
            {
                where = Context.GetAppKeyWhere("muser");
            }

            return true;
        }

        //加载修改数据
        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.listid ='{0}' ", this.Key));
            base.LoadDtlData(null, string.Format(" a.plistid ='{0}' ", this.Key));
        }

        protected override void AfterSystemChangeControlState()
        {
            if ("M" == this.Action)
            {
                //根据合同级别设区域和门店的启用与否
                string levercode = Validation.ToString(this.tbSummary.GetValue("levercode"));
                this.tbSummary.Enable("areacode", levercode == "1"); //区域代码：如果合同级别为区域则在此选择区域，否则为空;
                this.tbSummary.Enable("shopcode", levercode == "2");
            }
        }

        protected override void AfterCreateSummaryControl(string name, Control c)
        {
            if ("btncylce" == name)
            {
                Button btn = c as Button;
                btn.Click += new EventHandler(btn_Click);
            }
        }

        //付款周期
        void btn_Click(object sender, EventArgs e)
        {
            string key = Validation.ToString(this.tbSummary.GetValue("way1"));
            string value = Validation.ToString(this.tbSummary.GetValue("way1key1"));
            
            FrmBas1a40CEdit edit = new FrmBas1a40CEdit(key, value);
            edit.ShowDialog();

            if (!string.IsNullOrEmpty(edit.Key))
            {
                this.tbSummary.SetValue("way1", edit.Key);
                this.tbSummary.SetValue("way1key1", edit.Value);
            }
        }

        protected override bool Check()
        {
            if ("A" == this.Action)
            {
                string cntcode = Components.Validation.ToString(this.tbSummary.GetValue("cntcode")); //合同代码
                //由于合同代码可以不填写，在不填写的时候不验证合同号的存在性
                if (string.IsNullOrEmpty(cntcode)) return true;

                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                hs["cntcode"] = cntcode;
                hs["%muser"] = UI.Lib.Context.GetAppKeyWhere();
                bool reflt = UI.Lib.Factory.CommonHelper.Entity.DataInData("Bas1a40", hs);
                if (reflt)
                {
                    MessageBox.Show("当前合同代码已经存在！", "提示");
                    this.tbSummary.Focus("cntcode");
                    return false;
                }

                return true;
            }

            return true;
        }
    }
}
